#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;

namespace fastio{
	struct{template<typename T>operator T(){
		T x=0;char f=0,c=getchar();
		while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}
		while(c>='0'&&c<='9'){x=x*10+(c^48);c=getchar();}
		return f?-x:x;
	}}in;int stk[40],tp;
	template<typename T>void out(T x,char c=0){
		if(x<0)putchar('-'),x=-x;
		do stk[++tp]=x%10,x/=10;while(x);
		while(tp)putchar(stk[tp--]^48);
		if(c)putchar(c);
	}
}using fastio::in;using fastio::out;

const int N = 405;
int a[N],b[N],f[N][N][3],s[N];
vector<int> g[N];
int p[N];
int n;

signed main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	n = in;
	for(int k=1;k<=n;k++){
		a[k] = in;
		s[k] = s[k-1]+a[k];
		p[k] = k;
	}
	for(int k=1;k<n;k++){
		int a = in,b = in;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int ans=-1e18;
	do{
		memcpy(b,a,sizeof(b));
		int res = 0;
		for(int k=1;k<=n;k++){
			res += b[p[k]];
			for(int j:g[p[k]])
				b[j] += b[p[k]];
		}
		ans = max(ans,res);
	}while(next_permutation(p+1,p+1+n));
	out(ans);
	return 0;
}
